package com.smile.chatview;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListCellRenderer;

import com.smile.model.FriendInfo;

public class FriendItem extends JPanel implements ListCellRenderer<Object> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HeadImage headLabel;
	private JLabel nameLabel;
	private JLabel messageLabel;

	/**
	 * Create the panel.
	 */
	public FriendItem() {

		headLabel = new HeadImage("dzq3.jpg");

		nameLabel = new JLabel("name");
		nameLabel.setForeground(new Color(171, 171, 171));
		nameLabel.setFont(new Font("宋体", Font.BOLD, 16));

		messageLabel = new JLabel("message");
		messageLabel.setForeground(new Color(126, 126, 126));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(headLabel, GroupLayout.PREFERRED_SIZE,
								55, GroupLayout.PREFERRED_SIZE)
						.addGroup(
								groupLayout
										.createParallelGroup(Alignment.LEADING)
										.addComponent(messageLabel)
										.addComponent(nameLabel))
						.addContainerGap(68, Short.MAX_VALUE)));
		groupLayout
				.setVerticalGroup(groupLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								groupLayout
										.createSequentialGroup()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				headLabel,
																				GroupLayout.PREFERRED_SIZE,
																				55,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGap(14)
																		.addComponent(
																				nameLabel)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addComponent(
																				messageLabel)))
										.addGap(3)));
		setLayout(groupLayout);

	}

	@Override
	public Component getListCellRendererComponent(JList<? extends Object> list,
			Object value, int index, boolean isSelected, boolean cellHasFocus) {

		setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

		if (value != null) {
			String text = ((FriendInfo) value).getName();
			FontMetrics metrics;
			metrics = nameLabel.getFontMetrics(nameLabel.getFont());
			int len = 0;
			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < text.length(); ++i) {
				len += metrics.stringWidth(text.charAt(i) + "");
				if (len <= 97) {
					builder.append(text.charAt(i));
				} else {
					builder.append("...");
					break;
				}
			}
			nameLabel.setText(builder.toString());

			text = ((FriendInfo) value).getName();
			metrics = messageLabel.getFontMetrics(messageLabel.getFont());
			len = 0;
			builder = new StringBuilder();
			for (int i = 0; i < text.length(); ++i) {
				len += metrics.stringWidth(text.charAt(i) + "");
				if (len <= 98) {
					builder.append(text.charAt(i));
				} else {
					builder.append("...");
					break;
				}
			}
			messageLabel.setText(builder.toString());
			headLabel.setImagePath(((FriendInfo) value).getImgPath());
		}

		setForeground(new Color(169, 172, 173));
		if (isSelected) {
			setBackground(new Color(65, 73, 84));
			setForeground(new Color(255, 255, 255));
			// setForeground(list.getSelectionForeground());
		} else {
			// 设置选取与取消选取的前景与背景颜色.
			setBackground(list.getBackground());
			// setForeground(list.getForeground());
		}

		return this;
	}
}
